package cn.le.netty.messagepack;

import cn.le.netty.beans.NettyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.msgpack.MessagePack;

public class MessagePackEncoder extends MessageToByteEncoder<NettyMsg> {
    /**
     * 编码发送到NettyServer的消息,类型为NettyMsg
     * @param channelHandlerContext
     * @param msg
     * @param byteBuf
     * @throws Exception
     */
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, NettyMsg msg, ByteBuf byteBuf) throws Exception {
        MessagePack pack = new MessagePack();
        byte[] data = pack.write(msg);
        byteBuf.writeBytes(data);
    }
}
